package com.qnw.CardGroupManagement;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.ygo.feihua.Gongju;
import com.ygo.feihua.R;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.exception.BmobException;
import java.util.Map;
import com.ygo.feihua.adapter.AdapterAddcard;
import java.util.ArrayList;
import java.util.List;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.content.Intent;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.util.*;
import com.qnw.CardGroupManagement.adapter.*;
import android.support.v7.widget.*;
import com.ygo.feihua.service.*;

public class ComboAdd extends AppCompatActivity
{

	private Button ca_addcard;
	private RecyclerView ca_rec;
	private EditText ca_message;
	
	private CardListAdapter cardListAdp;
	
	private Gongju gj;
	private String km;
	DialogUtils du;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.combo_add);
		
		Intent intent=getIntent();
		km=intent.getStringExtra("km");
		du=DialogUtils.getdx(this);
		
		
		csh();
		ca_addcard.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					DialogUtils.CardList cl= du.dialogCxCards();
					cl.setOnCardItemClickListener(new DialogUtils.OnCardItemClickListener(){

							@Override
							public void onClick(Card card)
							{
								cardListAdp.add(card);
								du.dis();
								
								// TODO: Implement this method
							}
						});
					
					
					// TODO: Implement this method
				}
			});
		cardListAdp.setOnCardItemClickListener(new CardListAdapter.OnCardItemClickListener(){

				@Override
				public void onClick(Card card,final int position)
				{
					View[] vv=du.dialogt(false,"","是否删除该关联卡片?");
					Button b1,b2;
					b1=(Button) vv[0];
					b2=(Button) vv[1];
					b1.setText("取消");
					b2.setText("确定");
					b1.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								du.dis();
								// TODO: Implement this method
							}
						});
					b2.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								du.dis();
								cardListAdp.remove(position);
								// TODO: Implement this method
							}
						});
				
					
					// TODO: Implement this method
				}
			});
			
		
			
			
	}
	private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(MenuItem p1)
		{
			switch(p1.getItemId()){
				case R.id.ac_qd:
					String s=ca_message.getText().toString().trim();
					if(s.equals("")){
						gj.show("combo内容不能为空");
					}else{				
						List<AssociatedCard> la=new ArrayList<AssociatedCard>();
						for(Card card:cardListAdp.getData()){
							AssociatedCard ac=new AssociatedCard();
							ac.setName(card.getName());
							ac.setId(card.getId());
							la.add(ac);
						}
						
						CardInformation co=new CardInformation();
						co.setCard_type(0);
						co.setCard_message(s);
						co.setCard_sh(2);
						co.setCard_km(km);
						co.setCard_associated(la);
						co.save(new SaveListener<String>(){

								@Override
								public void done(String p1, BmobException p2)
								{
									if(p2==null){
										gj.show("添加成功");
										finish();
									}else{
										gj.show("添加失败,原因为"+p2);
									}
									// TODO: Implement this method
								}
							});
						
						
					}
					
					
					break;
			}


			// TODO: Implement this method
			return true;
		}
	};
	
	
	
	public void csh(){
		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		ca_addcard=(Button) findViewById(R.id.ca_addcard);
		ca_rec=(RecyclerView) findViewById(R.id.ca_rec);
		ca_message=(EditText) findViewById(R.id.ca_messgae);
		
		gj=Gongju.getdx(this);
		ca_rec.setLayoutManager(new LinearLayoutManager(this));
		ca_rec.addItemDecoration(new ServiceKacha.MyItemDecoration());
		cardListAdp=new CardListAdapter(this,new ArrayList<Card>());
		ca_rec.setAdapter(cardListAdp);
		gj.cshToolbar(tool,"添加combo");
		tool.setOnMenuItemClickListener(onMenuItemClick);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method

		getMenuInflater().inflate(R.menu.menu_addcombo, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	public static class AssociatedCard{
		private String name,id;


		public void setName(String name)
		{
			this.name = name;
		}

		public String getName()
		{
			return name;
		}

		public void setId(String id)
		{
			this.id = id;
		}

		public String getId()
		{
			return id;
		}}
	
}
